//
//  LSZDiscoverSearchView.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/12.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit

class LSZDiscoverSearchView: UIView, UITextFieldDelegate {
    
    @IBOutlet weak var searchTextFieldConstraints: NSLayoutConstraint!
    @IBOutlet weak var cancelButn: UIButton!
    
    @IBOutlet weak var searchTextField: UITextField!

//    类方法
    class func searchView() ->LSZDiscoverSearchView {
        return UINib(nibName: "LSZDiscoverSearchView", bundle: nil).instantiateWithOwner(nil, options: nil).last! as! LSZDiscoverSearchView
    }
    
    // MARK: textfield代理方法
    func textFieldDidBeginEditing(textField: UITextField) {
//        设置右边的间距等于按钮的宽度
        searchTextFieldConstraints.constant = cancelButn.width
//        设置约束动画
        UIView.animateWithDuration(0.3) { () -> Void in
//            重新布局
            self.layoutIfNeeded()
        }
    }
//    按钮的点击事件
    @IBAction func cancelAction(sender: AnyObject) {
//        取消第一响应者
        searchTextField.resignFirstResponder()
        
//        把布局还原
        searchTextFieldConstraints.constant = 0
        UIView.animateWithDuration(0.3) { () -> Void in
            //            重新布局
            self.layoutIfNeeded()
        }
    }
}
